package com.study.collection.ArrayList;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @ClassName CopyOnWriteArrayListDemo
 * @Author: jianghaok
 * @Description //CopyOnWriteArrayList解决ArrayList集合线程不安全问题
 * @Date: 2022/5/9
 * @Version 1.0
 */

// CopyOnWriteArrayList解决ArrayList集合线程不安全问题
public class CopyOnWriteArrayListDemo {
    public static void main(String[] args) {
        // CopyOnWriteArrayList解决
        List<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(0, 8));
                //从集合获取内容
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }
}
